package com.cd.javacxy.module.web.service.impl;

import com.cd.javacxy.common.model.entity.CxyMessage;
import com.cd.javacxy.common.model.enums.PageSize;
import com.cd.javacxy.common.model.enums.ResponseCode;
import com.cd.javacxy.common.model.query.MessageParams;
import com.cd.javacxy.common.model.query.MessageQuery;
import com.cd.javacxy.common.model.vo.MessageVo;
import com.cd.javacxy.common.utils.Page;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.exception.BaseException;
import com.cd.javacxy.module.web.dao.CxyMessageDao;
import com.cd.javacxy.module.web.service.CxyMessageService;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CxyMessage的服务接口的实现类
 *
 * @author yanghy
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CxyMessageServiceImpl implements CxyMessageService {

    private final CxyMessageDao cxyMessageDao;

    @Override
    public PageResult<MessageVo> findMessageByPage(MessageQuery messageQuery) {
        try {
            int pageSize = PageSize.PAGE_SIZE20.getSize();
            int count = findMessageCount(messageQuery);
            int pageNum = messageQuery.getPageNum();
            Page page = new Page(pageNum, count, pageSize);
            messageQuery.setPage(page);
            List<CxyMessage> result = cxyMessageDao.selectMessageList(messageQuery);
            List<MessageVo> messageVos = Lists.newArrayList();
            if (log.isDebugEnabled()) {
                log.debug("执行获取CxyMessage数据集-->结果:{}", result);
            }
            BeanUtils.copyProperties(result, messageVos);
            //List<Message> list = this.cxyMessageDao.selectList(messageQuery);
            return new PageResult<>(page, messageVos);
        } catch (Exception e) {
            log.error("获取消息列表出错", e);
            throw new BaseException("获取消息列表出错,请重试", ResponseCode.SERVERERROR.getCode());
        }
    }

    @Override
    public int findMessageCount(MessageQuery messageQuery) {
        return this.cxyMessageDao.selectMessageCount(messageQuery);
    }

    @Override
    public void delMessage(Integer userId, Integer[] ids) {
        try {
            if (ids == null || userId == null || ids.length == 0) {
                throw new BaseException("参数错误");
            }
            int result = this.cxyMessageDao.deleteCxyMessageByUserId(userId, ids);
            if (log.isDebugEnabled()) {
                log.debug("执行通过CxyMessage的id删除CxyMessage-->结果:{}", result);
            }
        } catch (Exception e) {
            log.error("消息删除出错", e);
            throw new BaseException("消息删除出错", ResponseCode.SERVERERROR.getCode());
        }
    }

    @Override
    public MessageVo getMessageById(Integer id, Integer userId) {
        MessageQuery messageQuery = new MessageQuery();
        messageQuery.setReceivedUserId(userId);
        messageQuery.setId(id);
        CxyMessage cxyMessage = this.cxyMessageDao.selectCxyMessageByUserId(messageQuery);
        MessageVo messageVo = new MessageVo();
        BeanUtils.copyProperties(cxyMessage, messageVo);
        return messageVo;
    }

    @Override
    public int update(Integer[] ids, Integer userId) {
        if(ids == null || userId == null || ids.length == 0){
            throw new BaseException("参数错误");
        }
        return this.cxyMessageDao.updateByUserId(userId, ids);
    }

    @Override
    public void createMessage(MessageParams messageParams) {

    }
}